package com.wzy.business.util

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.JPushMessage
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.luck.picture.lib.entity.LocalMedia
import com.wangzhenyu.common.base.bean.UserManager
import com.wangzhenyu.common.util.log.LogUtil
import com.wzy.business.base.ShopStatusConstants
import com.wzy.business.store.main.MainActivity
import com.wzy.business.store.main.ShopWaitApplyActivity
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File

object OtherUtils {

    /**
     * 复制到剪切板
     */
    fun copyToClipData(info: String?) {
        if (TextUtils.isEmpty(info)) return
        val cm = Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("info", info)
        cm.setPrimaryClip(clipData)
        ToastUtils.showShort("复制成功")
    }

    /**
     *  跳转到拨号盘
     */
    fun goToDialApp(activity: Activity?, phone: String?) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("手机号码不能为空")
            return
        }
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        activity?.startActivity(intent)

    }

    fun setStatusBarTransparent(activity: Activity) {
        val window = activity.window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = Color.TRANSPARENT
        }
    }

    fun goToMainActivity() {
        val loginInfo = UserManager.getLoginInfo() ?: return
        if (ShopStatusConstants.isAuthed(loginInfo.shopStatus)) {
            ActivityUtils.startActivity(MainActivity::class.java)
        } else {
            ActivityUtils.startActivity(ShopWaitApplyActivity::class.java)
        }
    }

    fun setJPushAlias() {
        if (UserManager.getLoginInfo()?.shopId == null) {
            return
        }
        MainScope().launch {
            //init 后直接 set 操作有极大可能导致失败，可能会在回调里拿到 6022，6002 等，测试的时候可以做个 7、8 秒的延时，正式业务里一般配合用户注册使用，延时基本上够用
            delay(2000)
            //这个接口是覆盖逻辑，而不是增量逻辑。即新的调用会覆盖之前的设置。
            //第二个参数是用户自定义的操作序列号，同操作结果一起返回，用来标识一次操作的唯一性，推荐每次都用不同的数字序号。
            JPushInterface.setAlias(
                Utils.getApp(),
                1,
                "wzy_" + UserManager.getLoginInfo()?.mobile.toString()
            )
            delay(8000)
        }
    }

    fun getImageFile(localMedia: LocalMedia?): File? {
        val path: String? = if (Build.VERSION.SDK_INT >= 29) {
            localMedia?.path
        } else {
            localMedia?.path
        }
        return if (path != null) {
            File(path)
        } else {
            null
        }
    }

    fun tranPersonBeanToMap(request: PersonInfoRequest): HashMap<String, String?> {
        val map = HashMap<String, String?>()
        if (request.face != null) map["face"] = request.face
        if (request.uname != null) map["uname"] = request.uname
        if (request.password != null) map["password"] = request.password
        if (request.oldPassword != null) map["oldPassword"] = request.oldPassword
        if (request.smsCode != null) map["smsCode"] = request.smsCode
        if (request.mobile != null) map["mobile"] = request.mobile
        return map
    }
}


data class PersonInfoRequest(
    var face: String? = null,
    var uname: String? = null,
    var id: String? = null,
    var password: String? = null,
    var oldPassword: String? = null,
    var smsCode: String? = null,
    var mobile: String? = null
)